<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2016/12/1
 * Time: 上午11:43
 */
class MY_Controller extends CI_Controller{
    public $user_data = array();
    public $offset = 12;
    public $jump_url;
    public $is_manage = 0;

    function __construct() {
        parent::__construct();
        $this->jump_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/index';
        $curr_url = 'http://'.$_SERVER['HTTP_HOST'].str_replace('/index.php','',$_SERVER['PHP_SELF']);
        // echo str_replace('/index.php','',$_SERVER['PHP_SELF']);
        if(empty($_SERVER['QUERY_STRING'])==false){
            $curr_url = $curr_url.'?'.$_SERVER['QUERY_STRING'];
        }
        $this->load->model('sch_privileges');
        $this->privilege_list = $this->sch_privileges->get_list(1);
        if(strstr($_SERVER['PHP_SELF'],"login")==false)
            $this->session->set_userdata('curr_url', $curr_url);

        $this->load->library('session');
        $this->user_data = $this->session->userdata('user_data');

        if(empty($this->user_data)){
//            print_r("null");
            // redirect('/login');exit;
            //用户存在
            header("Location:"."http://{$_SERVER['HTTP_HOST']}/login");
            exit;
        }else{
            $this->check_school_date();
            $this->load->model('User_model');
            $user = $this->User_model->get_user_by_id($this->user_data['id']);
            if(empty($user))
            {
                unset(
                    $_SESSION['id'],
                    $_SESSION['head_image'],
                    $_SESSION['user_name'],
                    $_SESSION['school_name'],
                    $_SESSION['school_id'],
                    $_SESSION['teacher_id'],
                    $_SESSION['mobile'],
                    $_SESSION['is_manager'],
                    $_SESSION['logged_in']
                );
                header("Location:"."http://".$_SERVER['HTTP_HOST']."/login");
                exit;
            }
            $is_manage = $this->User_model->check_school_manage($this->user_data['teacher_id']);
            $this->is_manage = $is_manage;
            if($is_manage == 0){
                //$this->check_user_privilege($this->user_data);
            }
            $this->check_privilege();
            $this->smarty->assign("is_manage", $is_manage);
            $this->smarty->assign("user_data", $this->user_data);
            //获取school菜单
            $this->load->model('School_manage_menu_model');
            $top_menu = $this->School_manage_menu_model->get_menu_list($is_manage);
            $this->smarty->assign("top_menu", $top_menu);
        }
    }

    function check_school_date(){
        $school_id = $this->user_data['school_id'];
        $school_info = get_field_by_where("o2o_school", "*", "id='{$school_id}'");
        $start_time = strtotime($school_info['start_date']);
        $end_time = strtotime($school_info['end_date']);
        $today_time = strtotime(date("Y-m-d"));
        if($start_time > $today_time || $end_time < $today_time){
            show_error("该网校运营时间已过期");exit;
        }
        $this->smarty->assign("school_info", $school_info);
    }

    function check_privilege(){

        $school_privileges_url['course/add_video'] = $school_privileges_url['course/edit_video'] = 'upload_video';
        $school_privileges_url['course_zhibo/publish'] = 'live';
        $school_privileges_url['course/shipinke_publish'] = $school_privileges_url['course/edit_shipinke'] = 'video_course';
        $school_privileges_url['course_banke/publish'] = $school_privileges_url['course_banke/edit'] = 'offline_course';
        $school_privileges_url['tiku/add_topic'] = $school_privileges_url['tiku/edit_topic'] = $school_privileges_url['tiku/import'] = 'add_topic';
        $school_privileges_url['tiku/add_test_volume'] = 'add_topic_volumes';

        $school_privileges = get_field_by_where("school_privileges", "*", "school_id='{$this->user_data['school_id']}'");
        if(empty($school_privileges)){
            $school_privileges['upload_video'] = $school_privileges['live'] = $school_privileges['video_course'] = $school_privileges['offline_course'] = $school_privileges['add_topic'] = $school_privileges['add_topic_volumes'] = $school_privileges['add_train'] = 0;
        }
        $url = $this->uri->rsegment(1) . '/' . $this->uri->rsegment(2);
        if(!empty($school_privileges_url[$url])){
            if($school_privileges[$school_privileges_url[$url]] == 0){
                show_error('您没有权限进行该操作！');exit;
            }
        }
    }

    function check_user_privilege($manage_user){
        $this->load->model("roles");
        $role_info = $this->roles->get_role_info($manage_user['teacher_id']);
        if($role_info['role_privilege'] == ''){
            show_message('/login', '您没有权限进入管理后台，请先联系超级管理员分配权限');
            exit;
        }else{
            $role_privilege = json_decode($role_info['role_privilege']);
            $manage_privilege = array();
            foreach ($this->privilege_list as $key => $value) {
                $sub = array();
                if(isset($value->sub) && count($value->sub) > 0){
                    foreach ($value->sub as $k => $val) {
                        if(in_array($val->menu_url, $role_privilege)){
                            $manage_privilege[$val->pid] = (object)array('id' => $value->id, 'pid' => $value->pid, 'action' => $value->action, 'menu_title' => $value->menu_title, 'menu_url' => $value->menu_url, 'sort' => $value->sort, 'add_time' => $value->add_time, 'status'=>$value->status);
                            $sub[] = $val;
                            
                        }
                    }
                    if(count($sub) > 0){
                        $manage_privilege[$val->pid]->sub = $sub;
                    }
                }
            }
            $manage_privilege = (object) $manage_privilege;
            $this->smarty->assign("menu_list", $manage_privilege);
            $url_total = $this->uri->total_segments();
            if($url_total >= 2){
                $user_privilege = $this->uri->segment(1) . '/' . $this->uri->segment(2);
            }else{
                $user_privilege = $this->uri->segment(1) . '/index';
            }
            $cls_methods = get_class_methods($this->uri->rsegment(1));
            if( ! in_array($user_privilege, $role_privilege ) && $this->uri->segment(2) != 'save' && $this->uri->segment(2) != 'get_qiniu_token'){
                if($this->is_ajax_request()){
                    echo json_encode(array('code' => 9, 'msg' => '您没有权限进行该操作！'));
                    exit;
                }else{
                    if(count($role_privilege) > 0){
                        show_message('/'.$role_privilege[0], '您没有权限进行该操作！');    
                    }else{
                        show_message('/manage_user/ignore_update_manage_user_password', '您当前没有任何权限！');
                    }
                    exit;
                }
            }
        }
    }

    function is_ajax_request(){
        if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) {
            return true;
        } else {
            return false;
        }
    }
}
